﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Web;
using System.Data;
using System.Data.Odbc;
using System.Configuration;
using System.Collections;

namespace WLMCoreLibrary
{


    public class ErrorManagement
    {
        private static List<RegistroError> _listaErrores;

        public static List<RegistroError> ListaErrores { get { return _listaErrores; } set { _listaErrores = value; } }

        public static void LogError(Exception ex, HttpContext contexto)
        {
            LogError(ex,contexto,null,null);
        }

        public static void LogError(Exception ex, HttpContext contexto,dbConnector conexion)
        {
            LogError(ex, contexto, conexion, null);
        }

        public static void LogError(Exception ex,HttpContext contexto,dbConnector conexion,OdbcCommand comando)
        {
            contexto.Response.Write(ex.Message + ex.StackTrace);
            //contexto.Response.End();


            if (ListaErrores == null)
                ListaErrores = new List<RegistroError>();

            StringBuilder cad = new StringBuilder();
            RegistroError errorNuevo = new RegistroError();
            errorNuevo.URL = contexto.Request.Url.Host+ contexto.Request.Url.AbsolutePath;
            errorNuevo.ErrorMessage = ex.Message;
            errorNuevo.StackTrace = ex.StackTrace;
            if (conexion != null)
                errorNuevo.SQLConnection = conexion.connector.ConnectionString;
            if(comando!=null)
                errorNuevo.SQLCommand = comando.CommandText;
            errorNuevo.QueryString = contexto.Request.QueryString.ToString();
            string[] cadena= contexto.Request.Form.ToString().Split('&');
            if(cadena.Length>2)
            for (int x = 2; x < cadena.Length; x++)
                cad.Append(cadena[x] + "&");
            errorNuevo.PostString = cad.ToString();
            ListaErrores.Add(errorNuevo);


            
        }

        
        


        public static void SendErrorLog()
        {
            StringBuilder html = new StringBuilder();
            html.Append("<div style='font-family:arial; font-size:11px;'><h3>Fecha de Envio de Errores: " + DateTime.Now.ToLongDateString() + " "+ DateTime.Now.ToLongTimeString()+"</h3>");
            foreach (RegistroError err in ListaErrores)
            {
                html.Append("<h2>"+err.URL+"</h2>"+
                    "<b>Mensaje Error:</b> "+ err.ErrorMessage+"<br/>"+
                    "<b>Comando SQL:</b> "+ err.SQLCommand+"<br/>"+
                    "<b>Conexion SQL:</b> "+ err.SQLConnection+"<br/>"+
                    "<b>Query String:</b> "+HttpContext.Current.Server.UrlDecode( err.QueryString)+"<br/>"+
                    "<b>POST String:</b> "+ HttpContext.Current.Server.UrlDecode( err.PostString)+"<br/>"+
                    "<b>StackTrace:</b> "+ err.StackTrace.Replace("\n","<br>")+"<hr/>" );
            }
            html.Append("</div>");
            //HttpContext.Current.Response.Write("LISTA DE ERRORES:<BR>"+html.ToString());
            HttpContext.Current.Response.Status = "500 Internal Server Error";
            List<MailBox> listaMails= new List<MailBox>();
            listaMails.Add( new MailBox("Reporte de Errores en sistemas","errores@wlm.com.mx"));

            //Mailer.Send(listaMails,"Error en sistema", html.ToString(),null);
            ListaErrores = new List<RegistroError>();
            //HttpContext.Current.Response.Write(html);
            HttpContext.Current.Response.End();
        }
    }


    public class RegistroError
    {
        private string _url;
        private string _querystring;
        private string _post;
        private string _SQLCommand;
        private string _stack;
        private string _SQLError;
        private string _SQLConn;
        private string _errorMassage;

        public string URL { get { return _url; } set { _url = value; } }
        public string QueryString { get { return _querystring; } set { _querystring = value; } }
        public string PostString { get { return _post; } set { _post = value; } }
        public string SQLCommand { get { return _SQLCommand; } set { _SQLCommand = value; } }
        public string SQLError { get { return _SQLError; } set { _SQLError = value; } }
        public string StackTrace { get { return _stack; } set { _stack = value; } }
        public string ErrorMessage { get { return _errorMassage; } set { _errorMassage = value; } }
        public string SQLConnection{ get { return _SQLConn; } set { _SQLConn = value; } }

        public RegistroError()
        {
        }
    }
}
